﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Logger4net;
using MS.Internal.Mita.Foundation;

namespace UIAStudio
{
    public class UIATreeNode : TreeNode
    {
        private UIObject _uiItem;

        public UIATreeNode(UIObject item)
            : base(GetNodeName(item))
        {
            _uiItem = item;
        }

        public static string GetNodeName(UIObject uiItem)
        {
            if (uiItem == null)
            {
                return "";
            }
            string name = uiItem.Name;
            string className = uiItem.ClassName;
            return String.Format("\"{0}\" - {1}", name, className);
        }

        public UIATreeNode FindChild(UIObject item)
        {
            ThreadStart ts = delegate
            {
                base.Expand();
            };
            base.TreeView.Invoke(ts);

            foreach (TreeNode node in base.Nodes)
            {
                UIATreeNode uiaNode = node as UIATreeNode;
                if (uiaNode == null)
                {
                    return null;
                }
                if (uiaNode.UIObject == item)
                {
                    return uiaNode;
                }
            }
            return null;
        }

        //public void RefreshChildren()
        //{
        //    ThreadStart ts = delegate
        //    {
        //        base.TreeView.BeginUpdate();
        //        base.Nodes.Clear();
        //        foreach (UIObject item in _uiItem.Children)
        //        {
        //            UIATreeNode node = new UIATreeNode(item);
        //            base.Nodes.Add(node);
        //            if (item.FirstChild != null)    // add +, make it expandable
        //            {
        //                base.Nodes.Add(new UIATreeNode(null));
        //            }
        //        }
        //        base.TreeView.EndUpdate();
        //    };
        //    base.TreeView.Invoke(ts);
        //}

        public UIObject UIObject
        {
            get
            {
                return _uiItem;
            }
        }
    }
}
